﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using AutoMapper;
using BBICMS.Application.InputModels.Works;
using BBICMS.Model.Works;
using BBICMS.Resources;
using BBICMS.ViewModel;
using BBICMS.WebUI.Filters;
using BBICMS.Application.ViewModels;

namespace BBICMS.WebUI.Areas.Admin.Controllers
{
    [SIGAuth]
    public class SolutionController : BaseController
    {
        // GET: Admin/Solution
        #region 案例分类

        // GET: /bbi_Admin/Work/Types

        public ActionResult Index(int? page)
        {

            IEnumerable<Solution> solutionList = _unit.Solutions.GetAll().OrderByDescending(w => w.Importance).ThenByDescending(w => w.AddedDate);

            return View(solutionList);
        }


        public ActionResult Add()
        {
            var vSolution = new SolutionIM
            {
                Active = true,
                Importance = 0
            };

            return View("Add", vSolution);
        }


        [HttpPost]

        public JsonResult Add(SolutionIM solutionIM)
        {

            if (ModelState.IsValid)
            {
                var solution = Mapper.Map<Solution>(solutionIM);
                _unit.Solutions.Add(solution);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertCreateSuccess, EntityNames.Solution));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertCreateFailure, EntityNames.Solution));
            return Json(AR, JsonRequestBehavior.DenyGet);
           
        }


        public ActionResult Edit(int id)
        {
            var vType = _unit.Solutions.GetById(id);

           
            if (vType != null)
            {
                var solution = Mapper.Map<SolutionIM>(vType);
                return View(solution);
            }
            return View("NotFound");
        }

        [HttpPost]

        public JsonResult Edit(SolutionIM solutionIM)
        {

          
            //site.UpdatedBy = User.Identity.Name;
            //site.UpdatedDate = DateTime.Now;   

            if (ModelState.IsValid)
            {
                var solution = Mapper.Map<Solution>(solutionIM);
                solution.UpdatedDate = DateTime.Now;

                _unit.Solutions.Update(solution);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertUpdateSuccess, EntityNames.Solution));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertUpdateFailure, EntityNames.Solution));
            return Json(AR, JsonRequestBehavior.DenyGet);

        }

        // DELETE: /User/DeleteSite
        [HttpDelete]
        public JsonResult Delete(int id)
        {

            Solution vType = _unit.Solutions.GetById(id);

            if (vType != null)
            {
                //string imgurl = Server.MapPath(vBlog.ImageURL);
                //if (System.IO.File.Exists(imgurl))
                //{
                //    System.IO.File.Delete(imgurl);    //删除缩略图
                //}


                _unit.Solutions.Delete(vType);
                _unit.SaveChanges();

                AR.SetSuccess(string.Format(Messages.AlertDeleteSuccess, EntityNames.Solution));
                return Json(AR, JsonRequestBehavior.DenyGet);
            }

            AR.Setfailure(string.Format(Messages.AlertDeleteFailure, EntityNames.Solution));
            return Json(AR, JsonRequestBehavior.DenyGet);
        }


        #endregion
    }
}